import { Injectable } from '@angular/core';
import { StateMachineService } from "@farris/command-services";
import { FrameContext } from '@farris/devkit';
import { BefRepository, ResponseInfo } from '@farris/bef';
import { FormMessageService, FormNotifyService, CommandService, FormLoadingService ,BeActionService} from '@farris/command-services';
import { EMPTY, of } from 'rxjs';
import { tap, map, switchMap } from 'rxjs/operators';
@Injectable()
export class ByjhpzlistFrmControllerService {
  constructor(
    private frameContext: FrameContext,
    private messageService: FormMessageService,
    private beActionService: BeActionService,
    public stateMachineService: StateMachineService,
    public formNotifyService: FormNotifyService,
    private commandService: CommandService,
    public loadingService: FormLoadingService
  ) {}

     /**
   * 刷新树组件
   * @remarks 
   * @returns 
   */
  reloadTree(): any {
    debugger
    // sblbRightTree-7x67-component
    this.commandService.execute("LoadTree1","sblbRightTree-7x67-component_sblbrighttree_tree-grid-component")
  }


    /**
   * 加入参数点击事件
   * @remarks 
   * @returns 
   */
  addparam(): any {
    let lbbh =  this.frameContext.viewModel.uiState['lbbh']
    const repository = this.frameContext.repository as BefRepository<any>;
    const requestInfo = repository.restService.buildRequestInfo();
    const actionUri = 'addparam';
    const body = {
      lbbh: lbbh,
      requestInfo: requestInfo
    }
    const action$ = this.beActionService.invokeAction(actionUri, 'PUT', null, null, body);
    return action$.pipe(
      tap((responseInfo: ResponseInfo) => {})
    ) 
  }


     /**
   * 加载二保数据
   * @remarks 
   * @returns 
   */
  loadchild2(): any {
    debugger
    this.commandService.execute("rootLoadSubList2")
  }

  /**
   * 校验主表数据
   * @remarks 
   * @returns 
   */
  checkMainData(): any {
    const checkeds = this.frameContext.appContext.componentManager.get(['data-grid-component','dataGrid']).checkeds;
    if(checkeds.length==0){
      this.messageService.warning('请选择设备！');
      return EMPTY;
    }
    let mainids: string[] = [];
      for(let item of checkeds){
        mainids.push(item.id);
    }
    debugger
    const repository = this.frameContext.repository as BefRepository<any>;
    const requestInfo = repository.restService.buildRequestInfo();
    const actionUri = 'isbypzgl';
    const body = {
      ids: mainids,
      requestInfo: requestInfo
    }
    const action$ = this.beActionService.invokeAction(actionUri, 'PUT', null, null, body);
    return action$.pipe(
      tap((responseInfo: ResponseInfo) => {
      })
    ) 
  }

 /**
   * 校验主表数据2
   * @remarks 
   * @returns 
   */
  checkMainData2(): any {
    const checkeds = this.frameContext.appContext.componentManager.get(['data-grid-component','dataGrid']).checkeds;
    if(checkeds.length==0){
      this.messageService.warning('请选择设备！');
      return EMPTY;
    }
    let mainids: string[] = [];
      for(let item of checkeds){
        mainids.push(item.id);
    }
    debugger
    const repository = this.frameContext.repository as BefRepository<any>;
    const requestInfo = repository.restService.buildRequestInfo();
    const actionUri = 'isbypzgl2';
    const body = {
      ids: mainids,
      requestInfo: requestInfo
    }
    const action$ = this.beActionService.invokeAction(actionUri, 'PUT', null, null, body);
    return action$.pipe(
      tap((responseInfo: ResponseInfo) => {
      })
    ) 
  }

 /**
   * 删除关联1
   * @remarks 
   * @returns 
   */
  deletechange1(): any {
    const data = this.frameContext.bindingData;
    const mainid = data.getValue(['id']);
    const checkeds = this.frameContext.appContext.componentManager.get(['ybgl-edcn-component','ybgl-edcn-dataGrid']).checkeds;
    let ids: string[] = [];
      for(let item of checkeds){
        ids.push(item.id);
    }
    debugger
    const repository = this.frameContext.repository as BefRepository<any>;
    const requestInfo = repository.restService.buildRequestInfo();
    const actionUri = 'deletechange';
    const body = {
      mainid: mainid,
      ids: ids,
      lx: '1',
      requestInfo: requestInfo
    }
    const action$ = this.beActionService.invokeAction(actionUri, 'PUT', null, null, body);
    return action$.pipe(
      tap((responseInfo: ResponseInfo) => {
      })
    ) 
  }


 /**
   * 删除关联2
   * @remarks 
   * @returns 
   */
  deletechange2(): any {
    const data = this.frameContext.bindingData;
    const mainid = data.getValue(['id']);
    const checkeds = this.frameContext.appContext.componentManager.get(['ebgl-deze-component','ebgl-deze-dataGrid']).checkeds;
    let ids: string[] = [];
      for(let item of checkeds){
        ids.push(item.id);
    }
    debugger
    const repository = this.frameContext.repository as BefRepository<any>;
    const requestInfo = repository.restService.buildRequestInfo();
    const actionUri = 'deletechange';
    const body = {
      mainid: mainid,
      ids: ids,
      lx: '2',
      requestInfo: requestInfo
    }
    const action$ = this.beActionService.invokeAction(actionUri, 'PUT', null, null, body);
    return action$.pipe(
      tap((responseInfo: ResponseInfo) => {
      })
    ) 
  }

   /**
   * 删除前校验
   * @remarks 
   * @returns 
   */
  deletecheck(): any {
    const checkeds = this.frameContext.appContext.componentManager.get(['data-grid-component','dataGrid']).checkeds;
    let ids: string[] = [];
      for(let item of checkeds){
        ids.push(item.id);
    }
    const repository = this.frameContext.repository as BefRepository<any>;
    const requestInfo = repository.restService.buildRequestInfo();
    const actionUri = 'deletecheck';
    const body = {
      mainids: ids,
      requestInfo: requestInfo
    }
    const action$ = this.beActionService.invokeAction(actionUri, 'PUT', null, null, body);
    return action$.pipe(
      tap((responseInfo: ResponseInfo) => {
      })
    ) 
  }
}
